--
-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--

---@type BTT_TryAttack_C
local M = UnLua.Class()

local ActionCharClass = UE.UClass.Load("/ActionPlatformer/Blueprints/Characters/BP_ActionChar.BP_ActionChar_C")

--- 收到执行AI
function M:ReceiveExecuteAI(OwnerController, ControlledPawn)
    self.CharacterAI = ControlledPawn:Cast(ActionCharClass)

    if not self.CharacterAI:CanAct() then
        self:FinishExecute(false)
    end

    self.CharacterAI.IsAttacking = true
    local Action = UE.UPaperZDPlaySlotOverrideAction.PlayAnimationOverrideWithCallbacks(
            self.CharacterAI:GetAnimInstance(), -- 获取当前实例的动画实例
            self.CharacterAI.AttackSequence[1], -- 指定要播放的攻击动画序列
            self.CharacterAI:GetWorld(), -- 获取当前实例所在的世界上下文
            "DefaultSlot" -- 动画播放的插槽名称
    )

    -- 绑定动画完成时的回调函数
    Action.OnCompleted:Add(self, self.AttackOnCompleted)

    -- 绑定动画被取消时的回调函数
    Action.OnCancelled:Add(self, self.AttackOnCancelled)

    -- 激活动画播放动作，开始播放攻击动画
    Action:Activate()
end

--- 攻击播放完成
function M:AttackOnCompleted()
    self:FinishExecute(true)
    self.CharacterAI.IsAttacking = false
    --  print("The attack animation is finished！(攻击动画播放完成)")
end

--- 攻击播放打断
function M:AttackOnCancelled()
    self:FinishExecute(false)
    self.CharacterAI.IsAttacking = false
    -- print("Animation cancelled!(攻击被打断了)")
end

return M